<!-- HTML header for doxygen 1.8.13-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.14"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Taskflow Handbook</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link rel="icon" type="image/x-icon" href="favicon.ico" />
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(initResizable);
/* @license-end */</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname"><a href="https://taskflow.github.io/">Taskflow</a>
   &#160;<span id="projectnumber">3.0.0-Master-Branch</span>
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.14 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search');
/* @license-end */
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
/* @license-end */</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('ForEachCUDA.html','');});
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Parallel Iterations (cudaFlow) </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>cudaFlow provides a template function that constructs a task to perform parallel iterations over a range of items.</p>
<h1><a class="anchor" id="ForEachCUDAIndexBasedParallelFor"></a>
Index-based Parallel Iterations</h1>
<p>Index-based parallel-for performs parallel iterations over a range <code>[beg, end)</code> with the given <code>step</code> size. The task created by <a class="el" href="classtf_1_1cudaFlow.html#ab5a7c12e383be4972844a9f29033e487" title="applies a callable to each index in the range with the step size ">tf::cudaFlow::for_each_index(I first, I last, I step, C&amp;&amp; callable)</a> represents a kernel of parallel execution for the following loop:</p>
<div class="fragment"><div class="line"><span class="comment">// positive step</span></div><div class="line"><span class="keywordflow">for</span>(<span class="keyword">auto</span> i=first; i&lt;last; i+=step) {</div><div class="line">  callable(i);</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">// negative step</span></div><div class="line"><span class="keywordflow">for</span>(<span class="keyword">auto</span> i=first; i&gt;last; i+=step) {</div><div class="line">  callable(i);</div><div class="line">}</div></div><!-- fragment --><p>Each iteration <code>i</code> is independent of each other and is assigned one kernel thread to run the callable. Since the callable runs on GPU, it must be declared with a <code>__device__</code> specifier. The following example creates a kernel that assigns each entry of <code>gpu_data</code> to 1 over the range <code></code>[0, 100) with step size 1.</p>
<div class="fragment"><div class="line">taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a60d7a666cab71ecfa3010b2efb0d6b57">emplace</a>([](<a class="code" href="classtf_1_1cudaFlow.html">tf::cudaFlow</a>&amp; cf){</div><div class="line">  <span class="comment">// ... create other gpu tasks</span></div><div class="line">  <span class="comment">// assigns each element in gpu_data to 1 over the range [0, 100) with step size 1</span></div><div class="line">  cf.<a class="code" href="classtf_1_1cudaFlow.html#ab5a7c12e383be4972844a9f29033e487">for_each_index</a>(0, 100, 1, [gpu_data] __device__ (<span class="keywordtype">int</span> idx) {</div><div class="line">    gpu_data[idx] = 1;</div><div class="line">  });</div><div class="line">});</div></div><!-- fragment --><h1><a class="anchor" id="ForEachCUDAIteratorBasedParallelIterations"></a>
Iterator-based Parallel Iterations</h1>
<p>Iterator-based parallel-for performs parallel iterations over a range specified by two iterators, <code>first</code> and <code>last</code>. The task created by <a class="el" href="classtf_1_1cudaFlow.html#a97c248490dbde983378f757239eaed4a" title="applies a callable to each dereferenced element of the data array ">tf::cudaFlow::for_each(I first, I last, C&amp;&amp; callable)</a> represents a parallel execution of the following loop:</p>
<div class="fragment"><div class="line"><span class="keywordflow">for</span>(<span class="keyword">auto</span> i=first; i&lt;last; i++) {</div><div class="line">  callable(*i);</div><div class="line">}</div></div><!-- fragment --><p>The two iterators, <code>first</code> and <code>last</code>, are typically two raw pointers to the first element and the next to the last element in the range in GPU memory space. The following example creates a <code>for_each</code> kernel that assigns each element in <code>gpu_data</code> to 1 over the range <code>[gpu_data, gpu_data + 1000)</code>.</p>
<div class="fragment"><div class="line">taskflow.<a class="code" href="classtf_1_1FlowBuilder.html#a60d7a666cab71ecfa3010b2efb0d6b57">emplace</a>([](<a class="code" href="classtf_1_1cudaFlow.html">tf::cudaFlow</a>&amp; cf){</div><div class="line">  <span class="comment">// ... create gpu tasks</span></div><div class="line">  <span class="comment">// assigns each element to 1 over the range [gpu_data, gpu_data + 1000)</span></div><div class="line">  cf.<a class="code" href="classtf_1_1cudaFlow.html#a97c248490dbde983378f757239eaed4a">for_each</a>(gpu_data, gpu_data + 1000, [] __device__ (<span class="keywordtype">int</span>&amp; item) {</div><div class="line">    item = 1;</div><div class="line">  }); </div><div class="line">});</div></div><!-- fragment --><p>Each iteration is independent of each other and is assigned one kernel thread to run the callable. Since the callable runs on GPU, it must be declared with a <code>__device__</code> specifier.</p>
<h1><a class="anchor" id="ForEachCUDAMiscellaneousItems"></a>
Miscellaneous Items</h1>
<p>The parallel-iteration algorithms are also available in <a class="el" href="classtf_1_1cudaFlowCapturerBase.html#a583e4df1139cf6f20b6e79dfde977a51" title="captures a kernel that applies a callable to each dereferenced element of the data array ...">tf::cudaFlowCapturer::for_each</a> and <a class="el" href="classtf_1_1cudaFlowCapturerBase.html#a0320596ee37d68cfce5746027ebc97de" title="captures a kernel that applies a callable to each index in the range with the step size ...">tf::cudaFlowCapturer::for_each_index</a>. </p>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="navelem"><a class="el" href="GPUAlgorithms.html">GPU Algorithms</a></li>
    <li class="footer">Generated by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.14 </li>
  </ul>
</div>
</body>
</html>
